package com.ccl.iot.huiju;

import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.GridView;
import android.widget.RelativeLayout;

import com.ccl.iot.R;
import com.ccl.iot.batch.IOTBatch;
import com.ccl.iot.bean.IOTHuiJuBean;
import com.ccl.iot.device.IOTDevice;
import com.ccl.iot.dvr.utils.LogUtil;
import com.ccl.iot.huiju.adapter.PositionItemAdapter;
import com.ccl.iot.huiju.db.IOTDaoUtil;
import com.ccl.iot.huiju.util.HuiJuConsts;
import com.ccl.iot.huiju.util.HuiJuUtil;
import com.ccl.iot.publics.UIUtils;

import java.util.ArrayList;
import java.util.List;


/**
 * 类说明：选择界面
 * 作者：HD on 2017/10/24 17:33
 * 邮箱：hehandong@qq.com
 */
public class PositionItemFragment extends BaseFragment {

    //-----------------UI--------------------
    private GridView mGridView;
    private PositionItemAdapter mPositionItemAdapter;
    private RelativeLayout mLeftLayout;
    private RelativeLayout mRightLayout;
    private Button mDeleteBtn;


    //-----------------Data--------------------

    public final static String TAG = "SelectItemFragment";

    private SparseArray<IOTHuiJu> mDataList;

    private int mPositionPageIndex;

    private int mEditPageIndex;

    public IOTHuiJu mIOTHuiJu;

    public OnPositionFragmentListener mFragmentListener;
    private String mIOTId;
    private String mName;
    private String mType;
    private long mDevId = -1;

    private ArrayList<IOTHuiJu> mIOTHuiJuList;

    public interface OnPositionFragmentListener {
        void onPositionFragmentDestroy(int page);
    }

    public void setOnPositionFragmentListener(OnPositionFragmentListener listener) {
        mFragmentListener = listener;
    }


    public PositionItemFragment() {
        // Required empty public constructor
    }


    public static PositionItemFragment newInstance() {
        PositionItemFragment fragment = new PositionItemFragment();
        return fragment;
    }

    @Override
    protected View setContentView(LayoutInflater inflater, ViewGroup container) {
        return inflater.inflate(R.layout.fragment_select_item, container, false);
    }

    @Override
    protected void initView(View view) {


        mDataList = new SparseArray<>();

        mGridView = (GridView) view.findViewById(R.id.gridView_select);

        mPositionItemAdapter = new PositionItemAdapter(mContext, mDataList);

        mGridView.setAdapter(mPositionItemAdapter);


        mLeftLayout = (RelativeLayout) view.findViewById(R.id.layout_left);

        mRightLayout = (RelativeLayout) view.findViewById(R.id.layout_right);

        mDeleteBtn = (Button) view.findViewById(R.id.btn_select_delete);


    }


    @Override
    protected void initListener() {


        mLeftLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LogUtil.i(TAG, "mLeftLayout");
                switchPage(mPositionPageIndex,false);

            }
        });

        mRightLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LogUtil.i(TAG, "mRightLayout");
                switchPage(mPositionPageIndex,true);
            }
        });

        mDeleteBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LogUtil.i(TAG, "mDeleteBtn");
                if (getActivity() instanceof OnPositionFragmentListener) {
                    ((OnPositionFragmentListener) getActivity()).onPositionFragmentDestroy(mEditPageIndex);
                }
            }
        });


        mPositionItemAdapter.setOnItemClickListener(new PositionItemAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {

               IOTDaoUtil.savePositionData(mIOTId,mName,mType,mDevId,mPositionPageIndex,position);

                if (getActivity() instanceof OnPositionFragmentListener) {
                    ((OnPositionFragmentListener) getActivity()).onPositionFragmentDestroy(mEditPageIndex);
                }

            }
        });


    }


    @Override
    protected void initData() {


    }


    @Override
    protected void onShow() {


    }

    @Override
    protected void onHide() {

    }


    public void setItemData(IOTHuiJu iotHuiJu,int page) {

        mIOTHuiJu = iotHuiJu;
        mEditPageIndex = page;
        
        if (getActivity() instanceof MainActivity){
            mIOTHuiJuList = ((MainActivity)getActivity()).getIOTHuiJuList();
        }
        if (iotHuiJu instanceof IOTDevice){
            IOTDevice iotDevice = (IOTDevice) iotHuiJu;
            mIOTId = iotDevice.getUDID();
            mName = iotDevice.getName();
            mDevId = iotDevice.getID();
            mType = HuiJuConsts.IOT_DEVICE;
        }else if (iotHuiJu instanceof IOTBatch){
            IOTBatch iotBatch = (IOTBatch) iotHuiJu;
            long id = iotBatch.getID();
            mIOTId = String.valueOf(id);
            mName = iotBatch.getName();
            mType = HuiJuConsts.IOT_BATCH;
        }

        int dataPageIndex = IOTDaoUtil.queryDataPage(mIOTId);

        refreshPage(dataPageIndex);

    }

    public void refreshPage(int pageIndex){

        if (pageIndex < 0){
            pageIndex = 0;
        }

        mDataList = HuiJuUtil.getPageData(pageIndex, mIOTHuiJuList);

        UIUtils.postTaskSafely(new Runnable() {
            @Override
            public void run() {
                if (mPositionItemAdapter != null){
                    mPositionItemAdapter.notifyDataSetChanged(mDataList);
                }
            }
        });

        mPositionPageIndex = pageIndex;

    }



    public void switchPage(int pageIndex, boolean isAdd) {

        if (isAdd) {
            List<IOTHuiJuBean> beanList = IOTDaoUtil.queryDataFromPage(pageIndex);
            if (beanList != null && beanList.size() > 0){
                pageIndex++;
                refreshPage(pageIndex);
            }
        } else {
            pageIndex--;
            refreshPage(pageIndex);
        }

    }


    //-----------------------业务逻辑----------------


}
